package com.xishu.upgrade;

import com.xishu.entity.order.CashPay;
import com.xishu.entity.order.Order;
import com.xishu.entity.order.OrderPayTypeMapping;
import com.xishu.entity.shop.PayType;
import com.xishu.service.CommonService;
import com.xishu.service.PayTypeService;
import com.xishu.util.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UpgradeOrderService implements Tools {
    private static Logger logger = LoggerFactory.getLogger(UpgradeOrderService.class);
    private static UpgradeOrderService instance = new UpgradeOrderService();
    private CommonService commonService = CommonService.getInstance();
    private PayTypeService payTypeService = PayTypeService.getInstance();

    private UpgradeOrderService() {
    }

    public static UpgradeOrderService getInstance() {
        return instance;
    }

    /**
     * 升级支付二进制
     *
     * @param orderInDb
     */
    public void upgradePayTypeBit(Order orderInDb) {
        //兼容性处理，如果不存在支付方式的二进制，则计算后保存
        if (orderInDb.getPayTypeBit() == null) {
            try {
                int payTypeBit = PayTypeService.getInstance().calOrderPayTypeBit(orderInDb);
                orderInDb.setPayTypeBit(payTypeBit);
                orderInDb.setPayTypeNameList(PayTypeService.getInstance().filterOrderPayTypeNameList(orderInDb));
                commonService.save(orderInDb);
            } catch (Exception e) {
                logger.error("e", e);
            }
        }
    }

    /**
     * 升级支付详细方式
     *
     * @param orderInDb
     */
    public void upgradeCashPay(Order orderInDb) {
        if (isEmpty(orderInDb.getCashPayList())) {
            return;
        }

        boolean upgrade = false;

        //cashPay list 支付类型兼容性处理
        try {
            for (CashPay cashPay : orderInDb.getCashPayList()) {
                if (isEmpty(cashPay.getPayTypeName_zh()) || isEmpty(cashPay.getPayTypeName_en())) {
                    if (getInt(cashPay.getPayType()) == 0) {
                        continue;
                    }

                    PayType payType = payTypeService.filterPayType(orderInDb.getShopId(), cashPay.getPayType());
                    cashPay.setPayTypeName_zh(payType.getName_zh());
                    cashPay.setPayTypeName_en(payType.getName_en());
                    upgrade = true;
                }
            }

            if (upgrade) {
                commonService.save(orderInDb);
            }
        } catch (Exception e) {
            logger.error("e", e);
        }
    }

    /**
     * 升级支付方式的显示
     *
     * @param orderInDb
     */
    public void upgradePayTypeName(Order orderInDb) {
        Integer payTypeInt = orderInDb.getPayType();

        if (payTypeInt == null) {
            return;
        }

        //父订单不用处理
        if (getBoolean(orderInDb.getParentOrder())) {
            return;
        }

        try {
            if (isEmpty(orderInDb.getPayTypeName_zh()) || isEmpty(orderInDb.getPayTypeName_en())) {
                PayType payType = payTypeService.filterPayType(orderInDb.getShopId(), payTypeInt);
                orderInDb.setPayTypeName_zh(payType.getName_zh());
                orderInDb.setPayTypeName_en(payType.getName_en());
                commonService.save(orderInDb);
            }
        } catch (Exception e) {
            logger.error("e", e);
        }
    }

    /**
     * 升级支付类型
     *
     * @param orderPayTypeMapping
     */
    public void upgradeOrderPayTypeMapping(OrderPayTypeMapping orderPayTypeMapping) {
        try {
            if (isEmpty(orderPayTypeMapping.getPayTypeName_zh()) || isEmpty(orderPayTypeMapping.getPayTypeName_en())) {
                Long payTypeInt = orderPayTypeMapping.getPayType();
                if (getLong(payTypeInt) == 0) {
                    return;
                }

                PayType payType = PayTypeService.getInstance().filterPayType(orderPayTypeMapping.getShopId(), Long.valueOf(payTypeInt).intValue());
                orderPayTypeMapping.setPayTypeName_zh(payType.getName_zh());
                orderPayTypeMapping.setPayTypeName_en(payType.getName_en());
                commonService.save(orderPayTypeMapping);
            }
        } catch (Exception e) {
            logger.error("e", e);
        }
    }
}
